package cn.exrick.xboot.modules.member.controller;

import cn.exrick.xboot.base.XbootBaseController;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.member.entity.Member;
import cn.exrick.xboot.modules.member.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Ydp
 */
@Slf4j
@RestController
@Api(tags = "会员用户管理接口")
@RequestMapping("/xboot/member")
@Transactional
public class MemberController {

    @Autowired
    private MemberService memberService;




    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<Member>> getByCondition(Member member, SearchVo searchVo, PageVo pageVo) {

        Page<Member> page = memberService.findByCondition(member, searchVo, PageUtil.initPage(pageVo));
        return new ResultUtil<Page<Member>>().setData(page);
    }



    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "通过id获取")
    public Result<Member> get(@PathVariable String id) {

        Member entity = memberService.get(id);
        return new ResultUtil<Member>().setData(entity);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取全部数据")
    public Result<List<Member>> getAll() {

        List<Member> list = memberService.getAll();
        return new ResultUtil<List<Member>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "分页获取")
    public Result<Page<Member>> getByPage(PageVo page) {

        Page<Member> data = memberService.findAll(PageUtil.initPage(page));
        return new ResultUtil<Page<Member>>().setData(data);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "保存数据")
    public Result<Member> save(Member entity) {
        System.out.println(entity);
        Member e = memberService.save(entity);
        System.out.println(e);
        return new ResultUtil<Member>().setData(e);
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value = "更新数据")
    public Result<Member> update(Member entity) {

        Member e = memberService.update(entity);
        return new ResultUtil<Member>().setData(e);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delByIds(String[] ids) {

        for (String id : ids) {
            memberService.delete(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }
}
